package com.learn.security.common.config;

import com.learn.security.modules.security.SecurityConstants;
import com.learn.security.modules.security.handler.JCAuthenticationFailureHandler;
import com.learn.security.modules.security.handler.JCAuthenticationSuccessHandler;
import com.learn.security.modules.security.handler.JCLogoutHandler;
import com.learn.security.modules.security.handler.JCLogoutSuccessHandler;
import com.learn.security.modules.security.sms.SmsCodeAuthenticationSecurityConfig;
import com.learn.security.modules.security.weixin.WeixinCodeAuthenticationSecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * created by yuanjunjie on 2019/6/10 2:35 PM
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private JCAuthenticationSuccessHandler authenticationSuccessHandler;
    @Autowired
    private JCAuthenticationFailureHandler authenticationFailureHandler;
    @Autowired
    private JCLogoutSuccessHandler logoutSuccessHandler;
    @Autowired
    private JCLogoutHandler logoutHandler;


    @Autowired
    private WeixinCodeAuthenticationSecurityConfig weixinCodeAuthenticationSecurityConfig;
    @Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.formLogin()
                .loginPage(SecurityConstants.LOGIN_URL_PAGE)
                .loginProcessingUrl(SecurityConstants.LOGIN_PROCESSING_URL_FORM)
                .successForwardUrl(SecurityConstants.LOGIN_SUCCESS_URL)
                .successHandler(authenticationSuccessHandler)
                .failureHandler(authenticationFailureHandler)
                .and()
                .apply(weixinCodeAuthenticationSecurityConfig)
                .and()
                .apply(smsCodeAuthenticationSecurityConfig)
                .and()
                .logout()
                .logoutUrl(SecurityConstants.LOGOUT_URL)
                .addLogoutHandler(logoutHandler)
                .logoutSuccessHandler(logoutSuccessHandler)
                .deleteCookies("JSESSIONID")
                .and()
                .authorizeRequests()
                .antMatchers(
                        SecurityConstants.LOGIN_URL_PREFIX + "/**",
                        SecurityConstants.CODE_URL_PREFIX + "/**",
                        SecurityConstants.WEIXIN_OAUTH2_PROCESSING_URL + "/**",
                        SecurityConstants.WEIXIN_REDIRECT_PREFIX + "/**",
                        SecurityConstants.WEIXIN_CONFIG_PRECESSING_URL + "/**",
                        SecurityConstants.WEIXIN_QR_CODE_URL + "/**",
                        SecurityConstants.WEIXIN_CONFIG_PRECESSING_URL + "/**"
                ).permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .csrf().disable().headers()
                .frameOptions().sameOrigin()
                .httpStrictTransportSecurity().disable();
    }

    /**
     * 不拦截静态资源
     * ,"/index.html","/eJiaBao/getUrl","/updateCarMumInfo.html"
     * @param web
     */
    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/static/**",
                "/js/**",
                "/css/**",
                "/libs/**",
                "/images/**",
                "/error",
                "/actuator/**");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
